package cn.hpclub.server.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;

/**
 * 根据环境变量env加载文件(-Denv=test)，从类路径下寻找，首先寻找classpath:config.name-${env}.ext，没找到就寻找${env}/config.name.ext，再没找到就找config.name.ext。
 * 没有设置env环境变量的时候，给什么路径就从什么路径找
 */
public class EnvProp {
    private static String env = System.getProperty("env");
    private static Logger logger = LoggerFactory.getLogger(EnvProp.class);
    /**
     * 根据一定的寻找策略在类路径下加载资源
     * @param fileName
     * @return InputStream 需要自己关闭流
     */
    public static  final InputStream loadEnvInputStream(String fileName){
        InputStream inputStream = null;
        ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
        if(env == null){//没有设置env环境变量的时候，给什么路径就从什么路径找
            inputStream = contextLoader.getResourceAsStream(fileName);
            logger.info("env = null , try to load " + fileName + " in classpath;");
            if(inputStream == null){
                throw new IllegalArgumentException("Properties file not found in classpath: " + fileName );
            }
        }else {
            String envFileName = getFileNameNoEx(fileName) + "-" + env + "." + getExtensionName(fileName);
            logger.info("env = " + env + " , try to load " + envFileName + " in classpath;");
            inputStream = contextLoader.getResourceAsStream(envFileName);//先找jdbc-${env}.properties
            if (inputStream == null) {
                String envDirName = env + File.separator + fileName;
                logger.info("env = " + env + " , try to load " + envDirName + " in classpath;");
                inputStream = contextLoader.getResourceAsStream(envDirName);//再找${env}/jdbc.properties
                if(inputStream == null){
                    logger.info("env = " + env + " , try to load " + fileName + " in classpath;");
                    inputStream = contextLoader.getResourceAsStream(fileName);//再找jdbc.properties
                    if(inputStream == null){
                        throw new IllegalArgumentException("Properties file not found in classpath: " + fileName + ", and " + envFileName + ",and " + envDirName);
                    }
                }

            }
        }
        return  inputStream;
    }
    /*
         * Java文件操作 获取文件扩展名
         */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }
    /*
     * Java文件操作 获取不带扩展名的文件名
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
